<?php

class Bootstrap
{
    const LOG_PRELOAD = true;
    const PRELOAD_FILE = '/var/preload.log';
    
    private static $_preloadList = array();

    public static function getPreloadFile()
    {
        return ROOT_PATH . self::PRELOAD_FILE;
    }
    
    public static function preloadLog($class)
    {
        self::$_preloadList[] = $class;
    }
    
    public static function preloadInit()
    {
        self::$_preloadList = explode("\n", file_get_contents(self::getPreloadFile()));
    }
    
    public static function preloadSave()
    {
        file_put_contents(self::getPreloadFile(), trim(implode("\n", array_unique(self::$_preloadList))));
    }
    
    public static function autoload($class)
    {
//        if (self::LOG_PRELOAD) {
//            self::preloadLog($class);
//        }
        require_once str_replace('_', '/', $class) . '.php';
    }

    public static function run()
    {
        // preload
//        if (self::LOG_PRELOAD) {
//            self::preloadInit();
//        }
        
        // redefine autoloader
//        require_once ROOT_PATH . '/var/preload.php';
        
        //$loader->registerNamespace('App');
        spl_autoload_unregister(array('Zend_Loader_Autoloader', 'autoload'));
        spl_autoload_register(array(__CLASS__, 'autoload'));

        include APPLICATION_PATH . '/code/Core/etc/application.php';
        $config = new Zend_Config($config);
        Zend_Session::start();
        $session = new Zend_Session_Namespace();
        Zend_Registry::set('session', $session);
        
        // configure View
        $view = new Zend_View(array(
            'basePath' => APPLICATION_PATH . '/code/Core/views/'
        ));
        Zend_Registry::set('view', $view);

        $layout = Zend_Layout::startMvc();
        $layout->setView($view);
        $layout->setLayoutPath(APPLICATION_PATH . '/code/Core/views/layouts');

        // configure db
        $db = Zend_Db::factory('Pdo_Mysql', $config->db);
        Zend_Db_Table::setDefaultAdapter($db);
        $db->query("SET NAMES 'utf8'");

        $front = Zend_Controller_Front::getInstance();
        // add controller dirs
        $front->setControllerDirectory(array(
            'Core' => APPLICATION_PATH . '/code/Core/controllers',
            'Bus' => APPLICATION_PATH . '/code/Bus/controllers',
            'Map' => APPLICATION_PATH . '/code/Map/controllers'
        ));
        $front->setParam('noViewRenderer', true);
        $front->setParam('displayExceptions', true);
        $front->setDefaultModule('Core');

        // configure router
        $router = new Zend_Controller_Router_Rewrite();

        $dispatcher = $front->getDispatcher();
        $request = $front->getRequest();
        $router->addRoute('route_admin_module', new App_Controller_Router_Route(
            array(), $dispatcher, $request
        ));
        $front->setRouter($router);

        // dispatch
        $front->dispatch();
        
        // preload
//        if (self::LOG_PRELOAD) {
//            self::preloadSave();
//        }
    }

}